#    Copyright 2011 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopip/djangoproject/include', '/opt/coopip/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from django import forms
import django, django.http
import djangoproject
from models import *
import COOPIP_Ajax
import COOPIP_Misc
import COOPIP_Form, COOPIP_DynamicTemplate, COOPIP_Authentication, COOPIP_FormRevenueProtection
import CIS, AMR
import Misc



def MainMenu(request):
    import datetime
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionTechnician', 'COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    form=COOPIP_Form.LoginForm()
    t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
    c = Context({'LoginStatus': LoginStatus, 'form': form, 'Permission': Permission})
    return HttpResponse(t.render(c))


def IncidentNoPermission():
    t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionincident.html')
    c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    return HttpResponse(t.render(c))


def Incident(request):
    import datetime
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):  
        return IncidentNoPermission() 
    SpecialBuildFormDataDict={'request': request}
    form=COOPIP_Form.BuildDynamicForm(request, 'RevenueProtectionIncident', SpecialBuildFormDataDict)
    Incident=COOPIP_FormRevenueProtection.ProcessIncidentForm(form, Username, request)
    
    #Now, rebuild the form in case a new incident was created, so that it will be selected
    if(Incident):
        SpecialBuildFormDataDict['Incident']=Incident
        form=COOPIP_Form.BuildDynamicForm(request, 'RevenueProtectionIncident', SpecialBuildFormDataDict)
    form=COOPIP_FormRevenueProtection.PopulateIncidentForm(form, Incident)
    if(Incident):
        Notes=djangoproject.models.revenueprotection.IncidentNote.objects.filter(Incident=Incident).order_by('-DateTime')
        Files=djangoproject.models.revenueprotection.IncidentFile.objects.filter(Incident=Incident).order_by('FileName')
        FileList=[]
        for file in Files:
            FileList.append([file.id, file.FileName])
        if(Incident.Point):
            Longitude=Incident.Point.x
            Latitude=Incident.Point.y
        else:
            Longitude=-80.7
            Latitude=33.7
    else:
        Notes=[]
        FileList=[]
        Longitude=-80.7
        Latitude=33.7
    PaymentList=[]
    if(Incident):
        Payments=djangoproject.models.revenueprotection.IncidentPayment.objects.filter(Incident=Incident).order_by('-DateTime')
        for i in Payments:
            PaymentList.append([i.Amount, i.DateTime, i.Description])
    
   
    t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionincident.html')
    c = Context({'LoginStatus': LoginStatus, 'form': form, 'Permission': Permission,
                 'Notes': Notes, 'FileList': FileList, 'Longitude': Longitude, 'Latitude': Latitude,
                 'PaymentList': PaymentList})
    return HttpResponse(t.render(c))

def IncidentFileView(request, IncidentFileid):
    import datetime
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, 'COOPIP_RevenueProtectionUser')
    try:
        incidentFile=djangoproject.models.revenueprotection.IncidentFile.objects.filter(id=IncidentFileid)[0]
    except:
        incidentFile=None
    if(not Permission) or (not incidentFile):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionincident.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))
    import mimetypes
    ContentType=mimetypes.guess_type(incidentFile.FileName)[0]
    LocalFile=open('/opt/coopip/media/incident/' + str(IncidentFileid))
    response=HttpResponse(content=LocalFile.read())
    response['Content-Type']=ContentType
    response['Content-Disposition']='attachment; filename=' + incidentFile.FileName
    return response

def Logout(request):
    django.contrib.auth.logout(request) 
    t=loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionlogout.html')
    c=Context({})
    return HttpResponse(t.render(c))







def MeterInvestigation(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    OpenLayersJavaScriptHeading=COOPIP_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPIP_Ajax.GetOpenLayersJavaScript()
    ActiveButNotAuthorizedMeters=Meter.objects.filter(Symbology='activebutnotauthorized').filter(CISIdleIndicator=True).order_by('-LastSuccessfulPingDateTime')
    InvestigationMeters=Meter.objects.filter(Symbology='investigation').order_by('-InvestigationStartDateTime')
    form=COOPIP_FormRevenueProtection.BuildAndProcessMeterInvestigationForm(request, ActiveButNotAuthorizedMeters, InvestigationMeters)
    COOPIP_DynamicTemplate.UpdateMeterInvestigationTemplate(ActiveButNotAuthorizedMeters, InvestigationMeters, Username)
    form = COOPIP_FormRevenueProtection.PopulateMeterInvestigationForm(form, ActiveButNotAuthorizedMeters, InvestigationMeters)
    NotAuthorizedReport=[]
    for meter in ActiveButNotAuthorizedMeters:
        row=[]
        row.append(meter.SerialNumber)
        DisconnectHistory=CIS.Locationdetl.objects.filter(meter=meter.SerialNumber.ljust(12,'0')).order_by('-discdate')
        if not DisconnectHistory.count():
            continue
        LastDisconnect=COOPIP_Misc.ConvertCISDisconnectDate(DisconnectHistory[0].discdate)
        row.append(LastDisconnect.strftime('%m/%d/%Y'))
        Read1Date, Read1KWH, Read2Date, Read2KWH = meter.GetLastTwoReads()

        #AMRMeterHistory=AMR.Meteracctstc.objects.filter(serialnumber=meter.SerialNumber).order_by('-metertcreaddt')[:2]
        row.append(Read1Date)
        row.append(Read1KWH)
        row.append(Read2Date)
        row.append(Read2KWH)

        NotAuthorizedReport.append(row)
    form.setData(form.data)
    t = loader.get_template('/opt/coopip/djangoproject/templates/dynamic_meterinvestigation' + Username + '.html')
    c = Context({'form': form, 'OpenLayersJavaScript': OpenLayersJavaScript, 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading,
                 'NotAuthorizedReport':NotAuthorizedReport})
    return HttpResponse(t.render(c))

def GenerateReport(ReportName, QuerySet, DisplayLimit=None):
    ReturnReport=[]
    if ReportName=='FailedMeterReport':
        for meter in QuerySet[:DisplayLimit]:
            row=[]
            row.append(meter.SerialNumber)
            row.append(meter.SubstationCountAlreadySearched)
            row.append(meter.PriorityLevel)
            row.append(meter.CompleteDateTime.strftime('%m/%d/%Y %H:%M'))
            ReturnReport.append(row)
    elif ReportName=='ResearchedMeterReport':
        for meter in QuerySet[:DisplayLimit]:
            row=[]
            row.append(meter.SerialNumber)
            row.append(meter.Substation.Name)
            row.append(meter.CompleteDateTime.strftime('%m/%d/%Y %H:%M'))
            FirstSearchOut=MeterSubstationSearch.objects.filter(MeterSearch=meter).order_by('StartDateTime')[0]
            Duration=meter.CompleteDateTime-FirstSearchOut.StartDateTime
            Minutes=str(int(Duration.seconds/60%60))
            Hours=str(int(Duration.seconds/60/60))
            if Duration.days > 0:
                row.append(str(Duration.days) + ' Days ' + Hours + ' Hours ' + Minutes + ' Min')
            else:
                row.append(Hours + ' Hours ' + Minutes + ' Min')
            ReturnReport.append(row)
    return ReturnReport
        


def SearchInMeters(request):
    """Allows addition of meters to TWACS search pool via batch file"""
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionTechnician', 'COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    form=COOPIP_Form.BuildDynamicForm(request, 'SearchInMeters')
    DisplayUnlimitedIndicator=COOPIP_FormRevenueProtection.ProcessSearchInMetersForm(form, Username, request)
    form=COOPIP_FormRevenueProtection.PopulateSearchInMetersForm(form)
    SubstationReport=[]
    SubstationHeadings=['Substation','Searches Out','Last Out','Last In']
    AMRSubstations=AMR.Scemain.objects.all().exclude(subname='UNKNOWN').order_by('subname')
    for amrsubstation in AMRSubstations:
        row=[]
        COOPIPSubstation=djangoproject.models.amrcache.Substation.objects.filter(Name=amrsubstation.subname)[0]
        row.append(amrsubstation.subname)
        SearchCount=AMR.Outcmd.objects.filter(subid=amrsubstation.subid).filter(funccode=300).count()
        row.append(str(SearchCount))
        SearchesOut=MeterSubstationSearch.objects.filter(Substation=COOPIPSubstation).exclude(OutToAMRDateTime__isnull=True).order_by('-OutToAMRDateTime')
        if SearchesOut.count():
            LastSearchOut=SearchesOut[0]
            row.append(LastSearchOut.OutToAMRDateTime.strftime('%m/%d %H:%M') + ': #' + str(LastSearchOut.MeterSearch.SerialNumber))
        else:
            row.append('')
        SearchesIn=MeterSubstationSearch.objects.filter(Substation=COOPIPSubstation).exclude(CompleteDateTime__isnull=True).order_by('-CompleteDateTime')
        if SearchesIn.count():
            LastSearchIn=SearchesIn[0]
            row.append(LastSearchIn.CompleteDateTime.strftime('%m/%d %H:%M') + ': #' + str(LastSearchIn.MeterSearch.SerialNumber))
        else:
            row.append('')
        SubstationReport.append(row)
    SummaryReport=[]
    SummaryHeadings=['Total Meters','Searches Out','Meters Added','Researched','Failed','Completed Last 24 Hours']
    MetersInQueue=MeterSearch.objects.filter(CompleteDateTime__isnull=True).exclude(SerialNumber__in=djangoproject.settings.FakeMeterSerialNumbers)
    SummaryReport.append(MetersInQueue.count())
    MetersSearching=MeterSearch.objects.filter(CurrentlyOutToAMRIndicator=True)
    SummaryReport.append(MetersSearching.count())
    MetersAdded=MeterSearch.objects.filter(FinalResultMeterAddedIndicator=True)
    SummaryReport.append(MetersAdded.count())
    MetersResearched=MeterSearch.objects.filter(FinalResultMeterResearchedIndicator=True)
    SummaryReport.append(MetersResearched.count())
    MetersFailed=MeterSearch.objects.filter(FinalResultMeterNotFoundIndicator=True)
    SummaryReport.append(MetersFailed.count())
    RecentSearches=MeterSearch.objects.filter(CompleteDateTime__gte=datetime.datetime.now()-datetime.timedelta(hours=24))
    SummaryReport.append(RecentSearches.count())
    if MeterSearch.objects.filter(CompleteDateTime__isnull=True).count():
        HighestPriorityMeter=MeterSearch.objects.filter(CompleteDateTime__isnull=True).order_by('-PriorityLevel')[0]
        HighestPriorityLevel=HighestPriorityMeter.PriorityLevel
        LowestPriorityMeter=MeterSearch.objects.filter(CompleteDateTime__isnull=True).order_by('PriorityLevel')[0]
        LowestPriorityLevel=LowestPriorityMeter.PriorityLevel
    else:
        HighestPriorityLevel=0
        LowestPriorityLevel=0
    AddedMeterReport=[]
    AddedMeters=MeterSearch.objects.filter(FinalResultMeterAddedIndicator=True).order_by('-CompleteDateTime')
    for meter in AddedMeters:
        row=[]
        row.append(meter.SerialNumber)
        try:
            SubstationName=meter.Substation.Name
        except AttributeError:
            SubstationName='ERROR: None'
        row.append(SubstationName)
        row.append(meter.PriorityLevel)
        try:
            CompletedDateTime=meter.CompleteDateTime.strftime('%m/%d/%Y %H:%M')
        except AttributeError:
            CompletedDateTime='ERROR: None'        
        row.append(CompletedDateTime)
        if not 'ERROR' in CompletedDateTime:
            Searches = MeterSubstationSearch.objects.filter(MeterSearch=meter)
            if not Searches.count():
                #make more robust
                row.append('No search out')
                continue
            FirstSearchOut=MeterSubstationSearch.objects.filter(MeterSearch=meter).order_by('StartDateTime')[0]
            Duration=meter.CompleteDateTime-FirstSearchOut.StartDateTime
            Minutes=str(int(Duration.seconds/60%60))
            Hours=str(int(Duration.seconds/60/60))
            if Duration.days > 0:
                row.append(str(Duration.days) + ' Days ' + Hours + ' Hours ' + Minutes + ' Min')

            else:
                row.append(Hours + ' Hours ' + Minutes + ' Min')
        AddedMeterReport.append(row)

    ResearchedMeters=MeterSearch.objects.filter(FinalResultMeterResearchedIndicator=True).filter(
                CompleteDateTime__gte=datetime.datetime.now() - datetime.timedelta(days=7)).order_by('-CompleteDateTime')
    FailedMeters=MeterSearch.objects.filter(FinalResultMeterNotFoundIndicator=True).filter(CompleteDateTime__gte=datetime.datetime.now()
                - datetime.timedelta(days=14)).order_by('-CompleteDateTime')
    if DisplayUnlimitedIndicator:
        ResearchedMeterReport=GenerateReport('ResearchedMeterReport', ResearchedMeters)    
        FailedMeterReport=GenerateReport('FailedMeterReport', FailedMeters)
    else:
        ResearchedMeterReport=GenerateReport('ResearchedMeterReport', ResearchedMeters, 500)
        FailedMeterReport=GenerateReport('FailedMeterReport', FailedMeters, 500)
  
    
    NewMeterReport=[]
    NewMeters=MeterSearch.objects.filter(FinalResultMeterAddedIndicator=True).filter(CompleteDateTime__gte=datetime.datetime.now()
             -datetime.timedelta(days=7)).order_by('Serial')
    MeterSpeedBySubstationReport=[]
    for substation in Substation.objects.all().exclude(Name='UNKNOWN').order_by('Name'):
        row=[]
        row.append(substation.Name)
        SubstationSearchesLastHour=MeterSubstationSearch.objects.filter(Substation=substation).filter(CompleteDateTime__gte=
                                      datetime.datetime.now() - datetime.timedelta(hours=1))
        SubstationSearchesLast24Hours=MeterSubstationSearch.objects.filter(Substation=substation).filter(CompleteDateTime__gte=
                                      datetime.datetime.now() - datetime.timedelta(hours=24))
        row.append(str(float(SubstationSearchesLastHour.count()))+ '/Hour')
        row.append(str(float(SubstationSearchesLast24Hours.count())) + '/Day')
        MeterSpeedBySubstationReport.append(row)
    SearchesOutToAMRReport=[]
    OngoingSubstationSearches=MeterSubstationSearch.objects.filter(OutToAMRIndicator=True).order_by('-MeterSearch__PriorityLevel','MeterSearch__SerialNumber')
    for search in OngoingSubstationSearches:
        row=[]
        row.append(search.MeterSearch.SerialNumber)
        row.append(search.Substation.Name)
        row.append(search.MeterSearch.PriorityLevel)
        try:
            LogData = amrcache.AMRSearchLogFile.objects.filter(RawData__icontains=search.MeterSearch.SerialNumber).order_by('-id')[0].RawData
        except:
            LogData='N/A'
        Duration=datetime.datetime.now() - search.StartDateTime
        Minutes=str(int(Duration.seconds/60%60))
        Hours=str(int(Duration.seconds/60/60))
        if Duration.days > 0:
            row.append(str(Duration.days) + ' Days ' + Hours + ' Hours ' + Minutes + ' Min')
        else:
            row.append(Hours + ' Hours ' + Minutes + ' Min')
        row.append(LogData)
        SearchesOutToAMRReport.append(row)
    t = loader.get_template('/opt/coopip/djangoproject/templates/searchinmeters.html')
    c = Context({'LoginStatus': LoginStatus, 'form':form, 'Permission':Permission, 'SubstationHeadings':SubstationHeadings,'SubstationReport':SubstationReport,
                 'SummaryHeadings':SummaryHeadings,'SummaryReport':SummaryReport,'HighestPriorityLevel':HighestPriorityLevel, 'LowestPriorityLevel':LowestPriorityLevel,
                 'SearchesOutToAMRReport':SearchesOutToAMRReport,'AddedMeterReport':AddedMeterReport, 'ResearchedMeterReport':ResearchedMeterReport, 
                 'FailedMeterReport':FailedMeterReport, 'MeterSpeedBySubstationReport': MeterSpeedBySubstationReport})
    return HttpResponse(t.render(c))

def SearchInMetersLog(request):
    """This report generates a table that has a list of the latest entries AMRSearchLog, MeterSubstationSearch, and MeterSearch tables"""
    import django.db
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    GenericReportTitle= 'Revenue Protection: SearchInMetersLog'
    Temp=[]
    StartDateTime=datetime.datetime.now() - datetime.timedelta(days=7)
    AMRSearchLogs=AMRSearchLogFile.objects.filter(CompleteDateTime__gte=StartDateTime).order_by('-CompleteDateTime')
    Temp.append('SerialNumber'.ljust(15) + 'ParseDateTime'.ljust(22) + 'CompleteDateTime'.ljust(22) + 'Added'.ljust(15) + 
                'Modified'.ljust(15) + 'NotFound'.ljust(15) + 'RawData')
    for i in AMRSearchLogs:
        Temp.append(str(i.SerialNumber).ljust(15) + str(i.ParseDateTime)[:19].ljust(22) + str(i.CompleteDateTime)[:19].ljust(22) + str(i.AddIndicator).ljust(15) + 
                    str(i.ModIndicator).ljust(15) + str(i.MeterNotFoundIndicator).ljust(15) + str(i.RawData))


    MeterSubstationSearches=MeterSubstationSearch.objects.filter(StartDateTime__gte=StartDateTime).order_by('-StartDateTime')
    Temp.append('')
    Temp.append('')

    Temp.append('SerialNumber'.ljust(15) + 'Substation'.ljust(25) + 'StartDateTime'.ljust(22) + 'CompleteDateTime'.ljust(22) + 
                'Wait'.ljust(15) + 'OutToAMR'.ljust(15) +
                'AMRDateTime'.ljust(22) + 'NotFound'.ljust(15) + 'Found'.ljust(15) + 'New'.ljust(15) + 
                'Researched'.ljust(15) + 'Update'.ljust(15))
    for i in MeterSubstationSearches:
        Temp.append(str(i.MeterSearch.SerialNumber).ljust(15) + str(i.Substation.Name).ljust(25) + str(i.StartDateTime)[:19].ljust(22) + str(i.CompleteDateTime)[:19].ljust(22) + 
                   str(i.WaitingToGoOutToAMRIndicator).ljust(15) + str(i.OutToAMRIndicator).ljust(15) +
                   str(i.OutToAMRDateTime)[:19].ljust(22) + str(i.AMRMeterNotFoundIndicator).ljust(15) + str(i.AMRNewMeterFoundIndicator).ljust(15) + 
                   str(i.AMRMeterResearchedIndicator).ljust(15) + str(i.MeterSearchNeedsToBeUpdatedIndicator).ljust(15))


    Temp.append('')
    Temp.append('')
    MeterSearches=MeterSearch.objects.filter(StartDateTime__gte=StartDateTime).order_by('-StartDateTime')
    Temp.append('SerialNumber'.ljust(15) + 'Substation'.ljust(25) + 'PriorityLevel'.ljust(20) + 'PrioritySubstation'.ljust(25) + 
                'StartDateTime'.ljust(22) + 'CompleteDateTime'.ljust(22) + 
                'OutToAMRCount'.ljust(15) + 'OutToAMR'.ljust(15) + 
                'LastOutToAMRDateTime'.ljust(22) + 'FinalNotFound'.ljust(15) + 'FinalAdded'.ljust(15) + 
                'FinalResearched'.ljust(15)  + 'SubstationCount'.ljust(15) + 'Searched')
        
    for i in MeterSearches:
        if(i.Substation):
            SubstationName=i.Substation.Name
        else:
            SubstationName = 'None'
        Temp.append(str(i.SerialNumber).ljust(15) + str(SubstationName).ljust(25) + str(i.PriorityLevel).ljust(20) + str(i.PrioritySubstation).ljust(25) + 
                    str(i.StartDateTime)[:19].ljust(22) + str(i.CompleteDateTime).ljust(22) + 
                    str(i.OutToAMRCount).ljust(15) + str(i.CurrentlyOutToAMRIndicator).ljust(15) + 
                    str(i.LastOutToAMRDateTime)[:19].ljust(22) + str(i.FinalResultMeterNotFoundIndicator).ljust(15) + str(i.FinalResultMeterAddedIndicator).ljust(15) + 
                    str(i.FinalResultMeterResearchedIndicator).ljust(15) + str(i.SubstationCountAlreadySearched).ljust(15) + str(i.SubstationNamesAlreadySearchedList))








    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))





def AppendMeterBlinkTableData(NumberOfBlinksToDisplay, Blinks, ResultList):
    RecentDates=MeterBlink().GetMostRecentDates()
    for i in range(0, NumberOfBlinksToDisplay):
        RawBlinkList=Blinks[i].GetSpecifiedRawCounts(RecentDates)
        RowList=[Blinks[i].Meter.SerialNumber, Blinks[i].BlinkCountTimeSpan, Blinks[i].BlinkWithNoNeighborTimeSpan, Blinks[i].BlinkDaysInRowTimeSpan,
                 Blinks[i].Meter.GISCircuitNumber]
        for j in range(0, len(RecentDates)):
            RowList.append(RawBlinkList[j])
        RowList.extend([Blinks[i].CISLastServiceOrderDate, Blinks[i].CISLastServiceOrderName, Blinks[i].Description])
        ResultList.append(RowList)


def MeterBlinkView(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        return IncidentNoPermission(request)
    try:
        ShowAllIndicator=bool(request.POST['ShowAllIndicator'] == 'on')
    except:
        ShowAllIndicator=False
    form=COOPIP_FormRevenueProtection.BuildAndProcessMeterBlinkForm(request)
    form=COOPIP_FormRevenueProtection.PopulateMeterBlinkForm(form)
    form.setData(form.data)
    TotalMetersWithBlinks=MeterBlink.objects.filter(BlinkCountTimeSpan__gte=1).count()
    BlinksToDisplay=int(TotalMetersWithBlinks*.05)
    RecentDates=MeterBlink().GetMostRecentDates()
    TableHeadingList=['Serial Number', 'Blinks', 'Alone', 'In Row', 'Circuit']
    for i in RecentDates:
        TableHeadingList.append(i)
    TableHeadingList.extend(['Service Order Date', 'Service Order Description', 'Notes'])
    RecentBlinks=[TableHeadingList]
    Blinks=list(MeterBlink.objects.all().select_related('Meter').order_by('-BlinkCountTimeSpan', '-BlinkWithNoNeighborTimeSpan', 'Meter__GISCircuitNumber'))
    AppendMeterBlinkTableData(BlinksToDisplay, Blinks, RecentBlinks)

    RecentBlinksWithoutNeighbors=[TableHeadingList]
    Blinks=list(MeterBlink.objects.all().order_by('-BlinkWithNoNeighborTimeSpan', '-BlinkCountTimeSpan', 'Meter__GISCircuitNumber'))
    AppendMeterBlinkTableData(BlinksToDisplay, Blinks, RecentBlinksWithoutNeighbors)

   


    

    #raise(Exception('Here'))
    t = loader.get_template('/opt/coopip/djangoproject/templates/meterblink.html')
    c = Context({'form': form, 'RecentBlinks': RecentBlinks, 'RecentBlinksWithoutNeighbors': RecentBlinksWithoutNeighbors})
    return HttpResponse(t.render(c))



def RateClassComparison(request):
    """This report generates a list of accounts based on unique serialnumbers from AMR, then compares their rate class to the matching account in CIS"""
    import django.db
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    GenericReportTitle= 'Revenue Protection: CIS/AMR Rate Comparison'
    Temp=[]
    MismatchedRate=[]
    NoAMRAccount=[]
    NoCISAccount=[]
    MatchedCount=0
    for i in AMRSerialNumber.objects.all():
        TWACSAccount=AMR.Meteraccts.objects.filter(serialnumber=i.serialnumber)
        if not TWACSAccount.count():
            NoAMRAccount.append(i)
            continue
        AMRAccount=TWACSAccount[0]
        AMRRate=AMRAccount.meterrateclass
        SEDCAccount=CIS.Memberdetl.objects.filter(meter=str(i.serialnumber).ljust(12,'0'))
        if not SEDCAccount.count():
            NoCISAccount.append(i)
            continue
        CISAccount=SEDCAccount[0]
        CISRate=CISAccount.class_field
        if CISRate == AMRRate.ljust(4,'0'):
            MatchedCount+=1
            continue
        Temp.append(str(i.serialnumber).ljust(20) + CISAccount.mbrsep.ljust(20) + CISRate.ljust(15) + AMRRate.ljust(15))
    Temp.insert(0,'Meter #'.ljust(20) + 'Member Sep'.ljust(20) + 'CIS Rate'.ljust(15) + 'AMR Rate'.ljust(15))
    Temp.insert(0, 'Matched: ' + str(MatchedCount))
    Temp.append('No Account Found in AMR for Serial #')
    for i in NoAMRAccount:
        Temp.append(str(i.serialnumber))
    Temp.append('No Account Found in CIS for Serial #')
    for i in NoCISAccount:
        Temp.append(str(i.serialnumber))
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))


def SearchMetersLists(request):
    """This report generates a list of meters with substations in multiple formats that can be copy/pasted in to search in meters"""
    import django.db
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    GenericReportTitle= 'Revenue Protection: Search Meters Lists'
    Temp=[]
    AMRMeters=AMRSerialNumber.objects.all().order_by('subid' , 'busid', 'fdrid')
    AMRSubstations={}  #AMRsubid : substation name
    for i in Substation.objects.all():
        AMRSubstations[str(i.AMRsubid)] = i.Name
    Temp.append('All Meters in AMRCache:')
    for i in AMRMeters:
        if(i.subid):
            Temp.append(str(i.serialnumber) + ':' + str(AMRSubstations[str(i.subid)]))
        else:
            Temp.append(str(i.serialnumber))
    Temp.append('')
    Temp.append('')
    PreviousSubid=-1
    PreviousBusid=-1
    PreviousFdrid=-1
    for i in AMRMeters:
        if(not PreviousSubid == i.subid) or (not PreviousBusid == i.busid) or (not PreviousFdrid == i.fdrid):
            Temp.append('')
            Temp.append('')

            Temp.append(('F: ' + str(i.busid)).ljust(15)  + ('C: ' + str(i.fdrid)).ljust(15))
            PreviousSubid = i.subid
            PreviousBusid = i.busid
            PreviousFdrid = i.fdrid
        if(i.subid):
            Temp.append(str(i.serialnumber) + ':' + str(AMRSubstations[str(i.subid)]))
        else:
            Temp.append(str(i.serialnumber))

    PreviousStatus = 'success'
    Meters=Meter.objects.all().exclude(Symbology='success').order_by('Symbology', 'Substation')
    for i in Meters:
        if(not PreviousStatus == i.Symbology):
            PreviousStatus=i.Symbology
            Temp.append('')
            Temp.append('')
            Temp.append(str(i.Symbology))
        if(i.Substation):
            Temp.append(str(i.SerialNumber) + ':' + str(i.Substation.Name))
        else:
            Temp.append(str(i.SerialNumber))

    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))

def SearchMetersAllCISMetersList(request):
    """This report generates a list of meters that exist in the CIS Meters Table [both meter numbers and meter dcsi numbers]"""
    import django.db
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    GenericReportTitle= 'Revenue Protection: All CIS Meters List'
    Temp=[]
    MetersList=[]
    MeterDetlList = list(CIS.Meterdetl.objects.all().values('meter', 'serial', 'amrid'))
    for i in MeterDetlList:
        if(not i['meter'].lstrip('0') in MetersList):
            MetersList.append(i['meter'].lstrip('0'))
        if(not i['serial'].lstrip('0') in MetersList):
            MetersList.append(i['serial'].lstrip('0'))
        if(not i['amrid'].lstrip('0') in MetersList):
            MetersList.append(i['amrid'].lstrip('0'))

    MeterDetlList = list(CIS.Meterhistdetl.objects.all().values('meter'))
    for i in MeterDetlList:
        if(not i['meter'].lstrip('0') in MetersList):
            MetersList.append(i['meter'].lstrip('0'))

    MeterDetlList = list(CIS.Servorderdetl.objects.all().values('meter'))
    for i in MeterDetlList:
        if(not i['meter'].lstrip('0') in MetersList):
            MetersList.append(i['meter'].lstrip('0'))

    MeterDetlList = list(CIS.Workcustdetl.objects.all().values('meter'))
    for i in MeterDetlList:
        if(not str(i['meter']).lstrip('0') in MetersList):
            MetersList.append(str(i['meter']).lstrip('0'))


    MeterDetlList = list(CIS.Emobldetl.objects.all().values('oldmeter', 'newmeter'))
    for i in MeterDetlList:
        if(not i['oldmeter'].lstrip('0') in MetersList):
            MetersList.append(i['oldmeter'].lstrip('0'))
        if(not i['newmeter'].lstrip('0') in MetersList):
            MetersList.append(i['newmeter'].lstrip('0'))


    for i in MetersList:
        if(not i.strip().replace('.', '') in MetersList):
            Temp.append(i.strip().replace('.', ''))
    Temp.sort()



    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))




def TruckMovement(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    OpenLayersJavaScriptHeading=COOPIP_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPIP_Ajax.GetAllMetersOpenLayersJavaScript(Top=False)
    form=COOPIP_Form.BuildDynamicForm(request, 'TruckMovement')
    COOPIP_FormRevenueProtection.ProcessTruckMovementForm(form)
    form=COOPIP_FormRevenueProtection.PopulateTruckMovementForm(form)
    #form.setData(form.data)
    t = loader.get_template('/opt/coopip/djangoproject/templates/truckmovement.html')
    c = Context({'form': form, 'OpenLayersJavaScript': OpenLayersJavaScript, 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading})
    return HttpResponse(t.render(c))

def DeleteFromAMR(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionTechnician', 'COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))
    GenericReportTitle='Revenue Protection: Delete From AMR'
    GenericMessage=''
    MetersWrong=['METERS WITH SERIALPORT NUMBERS OTHER THAN 0: ']
    SerialPorts=AMR.Serialport.objects.exclude(serialport=0)
    for i in SerialPorts:
        MetersWrong.append(str(i.serialnumber))
    form=COOPIP_Form.BuildDynamicForm(request, 'DeleteFromAMR')
    SerialNumber, ExecuteIndicator=COOPIP_FormRevenueProtection.ProcessDeleteFromAMRForm(form)
    form=COOPIP_FormRevenueProtection.PopulateDeleteFromAMRForm(form)
    ClassExcludeList=['admin', 'csv', 'datetime', 'models', 'os', 'reportlab', 'shutil', 'time']
    IntegerCheck=False
    try:
        Check=int(SerialNumber)
        IntegerCheck=True
    except:
        pass
    if ExecuteIndicator and IntegerCheck:
        AMR.Hourlygroupings.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Tempmeteraccts.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Meteraccts.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Meteracctsmit.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Serialnumber.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Serialport.objects.filter(serialnumber=SerialNumber).delete()
        AMR.Tempserialportref.objects.filter(serialnumber=SerialNumber).delete()
        #AMR.Twfuncgrpdet; Necessary for two-way addressing; disabled until confirmation that it doesn't adversely affect meters other than one specified
        Result = 'Commands Executed'
    else:
        Result = 'Execute Indicator not checked or invalid input.'
    MetersWrongPreformatted='\n'.join(MetersWrong)
    t = loader.get_template('/opt/coopip/djangoproject/templates/amrserialnumberdeletion.html')
    c = Context({'form': form, 'GenericReportTitle': GenericReportTitle, 'GenericMessage': GenericMessage, 'Result':Result,
                  'MetersWrong': MetersWrongPreformatted})
    return HttpResponse(t.render(c))


def RevenueProtectionReport(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))

    form=COOPIP_Form.BuildDynamicForm(request, 'RevenueProtectionReport')
    COOPIP_FormRevenueProtection.ProcessRevenueProtectionReportForm(form, Username)
    form = COOPIP_FormRevenueProtection.PopulateRevenueProtectionReportForm(form)


    RevenueProtectionReports = djangoproject.models.revenueprotection.RevenueProtectionReport.objects.all().order_by('-RunReportStartDateTime')
    for i in RevenueProtectionReports:
        i.FileNameWithoutPath = i.GetFileNameWithoutPath()
        i.TimeElasped = COOPIP_Misc.StringTimeSinceEvent(i.RunReportCompleteDateTime, i.RunReportStartDateTime)

    form.setData(form.data)
    t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionreport.html')
    c = Context({'form': form,  'RevenueProtectionReports':RevenueProtectionReports})
    return HttpResponse(t.render(c))


def RevenueProtectionReportGetPDF(request, FileName):
    ValidFileNameCharacters = '01234567890-_:.pdf'
    for character in FileName:
        if(not character in ValidFileNameCharacters):
            t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
            c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
            return HttpResponse(t.render(c))
    pdffile=open(djangoproject.settings.RevenueProtectionNightlyPDFReportLocation + FileName, 'rb').read()
    return django.http.HttpResponse(pdffile, 'application/DOWNLOADONLY')   #changed from 'application/pdf' to make the browser downlaod the file
    

def ViewVehicleHistory(request):
    """View that allows user to specify vehicle logs and view them on an interactive map"""
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionAdmin'])
    if(not Permission):  
        return Misc.DefaultMainPage(LoginStatus)
    SpecialBuildFormDataDict={}
    SpecialBuildFormDataDict['request'] = request
    form=COOPIP_Form.BuildDynamicForm(request, 'ViewVehicleHistory', SpecialBuildFormDataDict)

    ReturnDict = COOPIP_FormRevenueProtection.ProcessVehicleHistoryForm(form)
    try:
        FilteredLogs = ReturnDict['Logs']
        VehicleNumbers = ReturnDict['VehicleNumbers']
    except KeyError:
        FilteredLogs=[]
        VehicleNumbers=[]

    Headings = ['Time', 'Vehicle', 'Speed (MPH)', 'Direction', 'Transmission Status']
    Report=[]
    #reset from previous calls
    for i in FilteredLogs:
        Row=[]
        if i.SymbologyStopStartState:
            Row.append('emphasize')
        else:
            Row.append('normal')
        Row.append(i.CreateDateTime.strftime('%m/%d/%Y %H:%M'))
        Row.append(str(i.VehicleNumber))
        Row.append(str(i.Speed))
        Row.append(Misc.GetDirectionFromBearing(i.Bearing))
        Row.append(i.SymbologyStopStartState)
        Report.append(Row)
    form=COOPIP_FormRevenueProtection.PopulateVehicleHistoryForm(form)

    MapFileName = COOPIP_DynamicTemplate.UpdateVehicleLogMapFile(Username, VehicleNumbers)
    OpenLayersJavaScriptHeading=COOPIP_Ajax.GetOpenLayersJavaScriptHeading()
    OpenLayersJavaScript=COOPIP_Ajax.GetVehicleLogDisplayMapJavaScript(MapFileName)
    t = loader.get_template('/opt/coopip/djangoproject/templates/viewvehiclehistory.html')
    c = Context({'form': form, 'Report': Report, 'Headings': Headings,
                 'OpenLayersJavaScriptHeading': OpenLayersJavaScriptHeading,
                 'OpenLayersJavaScript': OpenLayersJavaScript})
    return HttpResponse(t.render(c))








def RevenueProtectionReportNote(request):
    import datetime
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):  
        return IncidentNoPermission() 
    SpecialBuildFormDataDict={'request': request}
    form=COOPIP_Form.BuildDynamicForm(request, 'RevenueProtectionReportNote', SpecialBuildFormDataDict)
    ProcessMessage = COOPIP_FormRevenueProtection.ProcessRevenueProtectionReportNoteForm(form, Username)
    form=COOPIP_FormRevenueProtection.PopulateRevenueProtectionReportNoteForm(form)
    Notes = []
    NoteMeters=Meter.objects.filter(RevenueProtectionReportNote__isnull=False).exclude(RevenueProtectionReportNote='').order_by('SerialNumber')
    for meter in NoteMeters:
        Notes.append([str(meter.SerialNumber), str(meter.RevenueProtectionReportNote)])


    t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotectionreportnote.html')
    c = Context({'LoginStatus': LoginStatus, 'form': form, 'Permission': Permission,
                 'Notes': Notes, 'ProcessMessage': ProcessMessage})
    return HttpResponse(t.render(c))







def FailedMeterData(request):
    """This report generates a table that has a list of the latest entries AMRSearchLog, MeterSubstationSearch, and MeterSearch tables"""
    import django.db
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_RevenueProtectionUser', 'COOPIP_RevenueProtectionAdmin'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/revenueprotection.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
    GenericReportTitle= 'Revenue Protection: Failed Meters'
    Report=[]
    Report.append('SerialNumber'.ljust(15) + 'Substation'.ljust(15) + 'Circuit'.ljust(15) + 'Phase'.ljust(10) + 'Ping DateTime'.ljust(25) 
                     + 'RSTNS'.ljust(10) + 'RSSCE'.ljust(10) + 'RSRCE'.ljust(10) + 'Outsub'.ljust(10) + 'Outbus'.ljust(10) + 'Outfdr'.ljust(10) + 'Outphs'.ljust(10) + 'Outsig'.ljust(10) + 'Outdet'.ljust(10))
    Meters = Meter.objects.filter(IncidentSymbology='failed').order_by('AMRSubstationName', 'AMRCircuitName')
    for i in Meters:
        Row=''
        Row += str(i.SerialNumber).ljust(15)
        Row += i.AMRSubstationName.ljust(15)
        Row += i.AMRCircuitName.ljust(15)
        Row += i.AMRPhaseCode.ljust(10)
        ping = MeterPing.objects.filter(SerialNumber=i.SerialNumber).order_by('-DateTime')[0]
        Row += str(ping.DateTime).ljust(25)
        Row += str(ping.Rstns).ljust(10)
        Row += str(ping.Rssce).ljust(10)
        Row += str(ping.Rsrce).ljust(10)
        Row += str(ping.Outsub).ljust(10)
        Row += str(ping.Outbus).ljust(10)
        Row += str(ping.Outfdr).ljust(10)
        Row += str(ping.Outphs).ljust(10)
        Row += str(ping.Outsig).ljust(10)
        Row += str(ping.Outdet).ljust(10)
        Report.append(Row)
    Preformatted='\n'.join(Report)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))

